package com.ld.admin.service.webbot;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;

/**
 * 
 * 机器人服务类，@Service注解用于自动注入
 * 
 * @ClassName SysUserNoteService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年07月02日 11:19:33
 *
 */
@Service
public class WebBotService {
    private TuringQueryService turingQueryService = new TuringQueryService();

    /**
     * Baidu bot query service.
     */
    private IBotService baiduQueryService = new IBotService();

    /**
     * ITPK query service.
     */
    private ItpkQueryService itpkQueryService = new ItpkQueryService();

    public Result send(int type, String content, String msgType) {
        Result result = new Result();
        String ret = "";
        String userName = "userName";
        String qqBotName = WebBotConfig.getString("bot.name"); // 改为实时获取机器人名称
        if (1 == type) {
            ret = turingQueryService.chat(userName, content);
            result.setSuccess(true);
            ret = StringUtils.replace(ret, "图灵机器人", qqBotName + "机器人");
            ret = StringUtils.replace(ret, "默认机器人", qqBotName + "机器人");

            ret = StringUtils.replace(ret, "<br>", "\n");
        } else if (2 == type) {
            ret = baiduQueryService.chat(content);
            result.setSuccess(true);
        } else if (3 == type) {
            ret = itpkQueryService.chat(content);
            result.setSuccess(true);
            // 2016-12-13kevin新增json格式处理
//            if (ret != null && ret.startsWith("{") && ret.endsWith("}")) {
//                Map<String, String> retMap = JSON.parseObject(ret, Map.class);
//                ret = retMap.get("title") + "\n" + retMap.get("content");
//            }
            // 如果是茉莉机器人，则将灵签结果格式化输出
            JSONObject parseMsg;
            if (ret == null) {
                ret = "";
            }
            if (ret.indexOf("\\u8d22\\u795e\\u7237\\u7075\\u7b7e") > 0) {
                // 财神爷灵签
                parseMsg = JSONObject.parseObject(ret);

                ret = "";
                ret += "第" + parseMsg.getString("number2") + "签<br>";
                ret += "签语: " + parseMsg.getString("qianyu") + "<br>";
                ret += "注释: " + parseMsg.getString("zhushi") + "<br>";
                ret += "解签: " + parseMsg.getString("jieqian") + "<br>";
                ret += "解说: " + parseMsg.getString("jieshuo") + "<br><br>";
                ret += "婚姻: " + parseMsg.getString("hunyin") + "<br>";
                ret += "事业: " + parseMsg.getString("shiye") + "<br>";
                ret += "运途: " + parseMsg.getString("yuntu");
                ret = ret.replace("null", "无");
            } else if (ret.indexOf("\\u89c2\\u97f3\\u7075\\u7b7e") > 0) {
                // 观音灵签
                parseMsg = JSONObject.parseObject(ret);

                ret = "";
                ret += "第" + parseMsg.getString("number2") + "签<br>";
                ret += "签位: " + parseMsg.getString("haohua") + "<br>";
                ret += "签语: " + parseMsg.getString("qianyu") + "<br>";
                ret += "诗意: " + parseMsg.getString("shiyi") + "<br>";
                ret += "解签: " + parseMsg.getString("jieqian");
                ret = ret.replace("null", "无");
            } else if (ret.indexOf("\\u6708\\u8001\\u7075\\u7b7e") > 0) {
                // 月老灵签
                parseMsg = JSONObject.parseObject(ret);

                ret = "";
                ret += "第" + parseMsg.getString("number2") + "签<br>";
                ret += "签位: " + parseMsg.getString("haohua") + "<br>";
                ret += "签语: " + parseMsg.getString("qianyu") + "<br>";
                ret += "注释: " + parseMsg.getString("zhushi") + "<br>";
                ret += "解签: " + parseMsg.getString("jieqian") + "<br>";
                ret += "白话释义: " + parseMsg.getString("baihua");
                ret = ret.replace("null", "无");
            }
        } else {
            result.setMessage("未定义的机器人类型！");
        }
        if (StringUtils.isBlank(ret)) {
            ret = "嗯~";
        }
        result.setData(ret);
        return result;
    }

}
